<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>分单查询</title>
        <% include("/common/common_css_js.html"){} %>
    </head>
    <body>
        <div style="padding-bottom: 5px;">
            <div id="masterBillCodeForm" class="uc-toolbaroverflow" _borderStyle="border-top:0;">
                <span>主单号：</span>
                <input id="keyId" name="keyId"  class="mini-textbox" required="false" />
                <span class="separator"></span>
                <span>使用状态：</span>
                <input id="keyUseStatusId" name="keyUseStatusId" class="mini-combobox" textField="name" valueField="id" emptyText="请选择..."
                       required="false" allowInput="false" showNullItem="true" nullItemText="请选择..." />

                <span class="separator"></span>
                <span>领单部门：</span>
                <input id="keyGenerateDepartmentId" name="keyGenerateDepartmentId" class="mini-treeselect" style="width:250px;" textField="name" valueField="id" emptyText="请选择..."
                       required="false" allowInput="true" showNullItem="true" nullItemText="请选择..."/>
                <span class="separator"></span>
                <span>航空公司：</span>
                <input id="keyXtAirlinesId" name="keyXtAirlinesId" class="mini-combobox" style="width:200px;" textField="name" valueField="id" emptyText="请选择..."
                       required="false" allowInput="true" showNullItem="true" nullItemText="请选择..."/>


                <div style="margin-top: 5px"></div>
<!--                value="${currentDate}"-->
                <span>领单日期：</span>
                <input id="keyStartGenerateDate" name="keyStartGenerateDate" class="mini-datepicker"   format="yyyy-MM-dd"/>--<input id="keyEndGenerateDate" name="keyEndGenerateDate" class="mini-datepicker"  format="yyyy-MM-dd"/>
                <span class="separator"></span>

                <span>领单人：</span>
                <input id="keyGenerateUserId" name="keyGenerateUserId" class="mini-combobox" style="width:200px;" textField="name" valueField="id" emptyText="请选择..."
                       required="false" allowInput="true" showNullItem="true" nullItemText="请选择..."/>
                <span class="separator"></span>
                <span>代理人：</span>
                <input id="keyAgentCompanyId" name="keyAgentCompanyId" class="mini-combobox" style="width:200px;" textField="name" valueField="id" emptyText="请选择..."
                       required="false" allowInput="false" showNullItem="true" nullItemText="请选择..."/>

                <% if(so.hasPermission("bMasterBillCode:query")){%>
                    <span class="separator"></span>
                    <a class="mini-button" iconCls="icon-add" onclick="searchCode()">查询</a>
                <% } %>

                <% if(so.hasPermission("bMasterBillCode:queryCndClear")){%>
                    <a class="mini-button" iconCls="icon-new" onclick="clearSearch()">清空查询条件</a>
                <% } %>

                <% if(so.hasPermission("bMasterBillCode:delete")){%>
                    <span class="separator"></span>
                    <a class="mini-button" iconCls="icon-remove" onclick="deleteMasterBillCode()">删除</a>
                <% } %>
                <% if(so.hasPermission("bMasterBillCode:download")){%>
                    <span class="separator"></span>
                    <#download_grid id="downloadGrid" theGridId="codeDatagrid" theFileName="主单号表"/>
                <% } %>
            </div>
        </div>

        <div class = "mini-fit">
            <div id="codeDatagrid" class="mini-datagrid"
                 style="width: 100%; height: 100%;"
                 url="${base}/pages/bill_code_manage/b_master/queryMasterBillCodeList" idField="id"
                 showPager="true" pageSize="20" sizeList="[20,30,40,50,100]"
                 multiSelect = "true"
                 allowResize="true"
                 allowCellWrap="true">
                <div property="columns">
                    <div type="checkcolumn" ></div>
                    <div field="id" width="100" headerAlign="center" align="center" allowSort="true">主单号</div>
                    <div field="airlinesId" width="140" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"airlines"}' renderer="commonRender">航空公司</div>
                    <div field="useStatusId" width="100" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"sysDic"}' renderer="commonRender">使用状态</div>
                    <div field="generateDepartmentId" width="100" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"department"}' renderer="commonRender">领单部门</div>
                    <div field="generateDate" width="100" headerAlign="center" align="center" allowSort="true" dateFormat="yyyy-MM-dd" renderer="ondayRenderer">领单日期</div>
                    <div field="generateUserId" width="100" headerAlign="center" align="center" data-options='{cacheName:"sysUser"}' renderer="commonRender">领单人</div>
                    <div field="authUseDepartmentId" width="100" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"department"}' renderer="commonRender">授权使用部门</div>
                    <div field="useDepartmentId" width="100" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"department"}' renderer="commonRender">使用部门</div>
                    <div field="useUserId" width="100" headerAlign="center" align="center" allowSort="true" data-options='{cacheName:"sysUser"}' renderer="commonRender">使用人</div>
                    <div field="agentCompanyId" width="160" headerAlign="center" align="left" allowSort="true" data-options='{cacheName:"sysCompany"}' renderer="commonRender">代理人</div>
                    <div width="100" headerAlign="center" align="center" renderer="showOpRenderer">操作</div>
                </div>
            </div>
        </div>

        <!--  主单授权使用部门调整 -->
        <div id="adjustUseDepartmentPanel" class="mini-window" title="主单授权使用部门调整" style="width:400px;height:200px;"
             showMaxButton="true" showCollapseButton="false" showMinButton="true" showShadow="true"
             showToolbar="true" showFooter="true" showModal="true" allowResize="true" allowDrag="true">

            <div property="footer" style="padding:10px;text-align: right">
                <a  class="mini-button" iconCls="icon-ok" onclick="saveAdjustUseDepartment()">保存</a>
                <a  class="mini-button" iconCls="icon-close" onclick="public_onCancel('adjustUseDepartmentPanel')">关闭</a>
            </div>


            <div style="margin-top: 5px"></div>
            <input id="masterBillCodeId" name="masterBillCodeId"  class="mini-hidden" required="false" />
            <span>主单授权使用部门选择:</span>
            <input id="useDepartmentId" name="useDepartmentId" class="mini-treeselect" style="width:200px;" textField="name" valueField="id" emptyText="请选择..."
                   required="true" allowInput="true" showNullItem="true" nullItemText="请选择..."/>
        </div>



        <script type="text/javascript">
            mini.parse();
            let masterBillCodeForm = new mini.Form("#masterBillCodeForm");
            let keyId = mini.get("keyId");
            let keyUseStatusId = mini.get("keyUseStatusId");
            let keyGenerateDepartmentId = mini.get("keyGenerateDepartmentId");
            let keyXtAirlinesId = mini.get("keyXtAirlinesId");
            let keyStartGenerateDate = mini.get("keyStartGenerateDate");
            let keyEndGenerateDate = mini.get("keyEndGenerateDate");
            let keyGenerateUserId = mini.get("keyGenerateUserId");
            let keyAgentCompanyId = mini.get("keyAgentCompanyId");
            let codeDatagrid = mini.get("codeDatagrid");


            let masterBillCodeId = mini.get("masterBillCodeId");
            let useDepartmentId = mini.get("useDepartmentId");


            $(function () {
                // 使用状态初始化
                let useStatusJsonData=commonRenderSysDicDropListByKey("sysDic","XT_DIC_USE_STATUS");
                keyUseStatusId.load(useStatusJsonData);
                // 部门下拉列表初始化
                let xtDepartmentJsonData=commonRenderDropList("department");
                keyGenerateDepartmentId.loadList(xtDepartmentJsonData,"id","pid");
                useDepartmentId.loadList(public_deepClone(xtDepartmentJsonData),"id","pid");

                // 航空公司初始化
                let xtAirlinesJsonData=commonRenderDropList("airlines");
                keyXtAirlinesId.load(xtAirlinesJsonData);
                // 生成人下拉列表
                let xtUserJsonData=commonRenderDropList("sysUser");
                keyGenerateUserId.load(xtUserJsonData);
                // 代理公司（代理人）初始化
                let agentCompanyJsonData=commonRenderDropList("sysCompany");
                keyAgentCompanyId.load(agentCompanyJsonData);

                // 加载列表数据
                codeDatagrid.load();
            });


            /**
             * 根据条件查询主单号
             */
            function searchCode() {
                let data = masterBillCodeForm.getData(true);
                // 交换一下日期，如果开始日期大于结束日期
                if(data.keyStartGenerateDate>data.keyEndGenerateDate){
                    let temp=data.keyStartGenerateDate;
                    data.keyStartGenerateDate = data.keyEndGenerateDate;
                    data.keyEndGenerateDate = temp;
                }
                // 加载数据
                codeDatagrid.load(data);
            }

            /**
             * 清空查询
             */
            function clearSearch() {
                let form = new mini.Form("#masterBillCodeForm");
                form.clear();
            }

            /**
             * UNUSED(241,"未使用"),
             USED(242,"已使用"),
             CANCELLED(243,"已作废");
             * @param e
             * @returns {string}
             */

            let useStatus = {unused:241,used:242,cancelled:243};

            function showOpRenderer(e){
                let record = e.record;
                let useStatusId = record.useStatusId;
                let masterBillId = record.id;
                // 根据使用状态判断是否显示按钮
                if(useStatusId == useStatus.unused){
                    return '<a class="mini-button" onclick="showAdjustUseDepartmentPanel(\'' + masterBillId + '\')" style="padding: 2px">调整授权使用部门</a>';
                }
            }

            /**
             * 展示授权使用部门调整面板
             * @param masterBillId
             */
            function showAdjustUseDepartmentPanel(masterBillId){
                masterBillCodeId.setValue(masterBillId);
                public_showModelAtPos("adjustUseDepartmentPanel", "center", "middle");
            }

            /**
             *
             */
            function saveAdjustUseDepartment(){

                let useDepartmentIdValue = useDepartmentId.getValue();
                if(public_isEmpty(useDepartmentIdValue)){
                    showAlert("请选择要调整的授权使用部门!");
                    return;
                }

                let useDepartmentIdName = useDepartmentId.getText();
                let masterBillCodeIdValue = masterBillCodeId.getValue();

                mini.confirm("确定调整主单号【" + masterBillCodeIdValue + "】使用部门为【" + useDepartmentIdName + "】?", "授权使用部门调整",
                    function (action) {
                        if (action == "ok") {
                            let ret = public_getServerData("${base}/pages/bill_code_manage/b_master/adjustUseDepartment",{masterBillCode:masterBillCodeIdValue,authorizationUseDepartmentId:useDepartmentIdValue});
                            showAlert(ret.message,function (){
                                if(ret.ok){
                                    codeDatagrid.reload();
                                    public_onCancel('adjustUseDepartmentPanel');
                                }
                            });
                        }
                    }
                );

            }


            /**
             * 240,0,使用状态,使用状态,XT_DIC_USE_STATUS
             241,240,未使用,未使用,""
             242,240,已使用,已使用,""
             */
            let userStatus = {noUse:241,used:242};

            function deleteMasterBillCode(){
                let selectItems = codeDatagrid.getSelecteds();
                //useStatusId
                if(selectItems.length == 0){
                    showAlert("请选择需要删除的主单号!");
                    return;
                }

                let canNotDeleteCodes = "";
                let canDeleteCodes = "";

                for(let obj of selectItems){
                    if(obj.useStatusId == userStatus.used){
                        canNotDeleteCodes += obj.id + ",";
                    }
                    else{
                        canDeleteCodes += obj.id + ",";
                    }
                }


                if(canNotDeleteCodes.length>0){
                    canNotDeleteCodes = canNotDeleteCodes.substr(0,canNotDeleteCodes.length-1);
                    showAlert("选择的主单号【" + canNotDeleteCodes +"】已经被使用，不能删除!");
                    return;
                }

                if(canDeleteCodes.length == 0){
                    return;
                }

                canDeleteCodes = canDeleteCodes.substr(0,canDeleteCodes.length-1);

                mini.confirm("确定删除已经选择的主单号？", "主单号删除",
                    function (action) {
                        if (action == "ok") {
                            let ret = public_getServerData("${base}/pages/bill_code_manage/b_master/deleteMasterBillCode",{deleteMasterBillCodes:canDeleteCodes});
                            showAlert(ret.message,function (){
                                if(ret.ok){
                                    codeDatagrid.reload();
                                }
                            });
                        }
                    }
                );
            }

        </script>
    </body>
</html>